package com.mbc.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mbc.mapper.LikeClassificationTableMapper;
import com.mbc.pojo.MbcLikeClassificationTable;
import com.mbc.service.LikeClassificationTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 点赞分类表 服务实现类
 * </p>
 *
 * @author suimeng
 * @since 2024-04-10
 */
@Service
public class LikeClassificationTableServiceImpl implements LikeClassificationTableService {

    @Autowired
    private LikeClassificationTableMapper likeClassificationTableMapper;

    /**
     * 分页获取点赞列表or分类查询点赞列表
     *
     * @param userId 用户 ID
     * @param code   法典
     * @param page   页
     * @param size   大小
     * @return {@link List}<{@link MbcLikeClassificationTable}>
     */
    @Override
    public PageInfo<MbcLikeClassificationTable> getLikeList(Integer userId, Integer code, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        if (code != null && code != 0){
            //code不为空就分类的查询点赞表
            List<MbcLikeClassificationTable> list1 = likeClassificationTableMapper.getLikeCodeList(userId, code);
            return new PageInfo<>(list1);
        }
        //code为空就查询全部点赞表
        List<MbcLikeClassificationTable> list2 = likeClassificationTableMapper.getLikeList(userId);
        return new PageInfo<>(list2);
    }

    /**
     * 新增点赞
     *
     * @param userId  用户 ID
     * @param likeUrl 喜欢 url
     * @param code    类型
     * @return boolean
     */
    @Override
    public boolean insertLike(Integer userId, String likeUrl, Integer code) {
        LocalDateTime now = LocalDateTime.now();
        return likeClassificationTableMapper.insertLike(userId,likeUrl,code,now) > 0;
    }

    /**
     * 取消点赞
     *
     * @param userId  用户 ID
     * @param likeUrl 喜欢 url
     * @return boolean
     */
    @Override
    public boolean deleteLike(Integer userId, String likeUrl) {
        return likeClassificationTableMapper.deleteLike(userId,likeUrl) > 0;
    }
}
